package com.qk.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.Response.PageResult;
import com.qk.dto.CoursePageDTO;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public PageResult<Course> getPageCourses(CoursePageDTO coursePageDTO) {
        //参数校验
        coursePageDTO.checkPageParam();
        PageHelper.startPage(coursePageDTO.getPage(),coursePageDTO.getPageSize());
        List<Course> courses = courseMapper.selectPageCourses(coursePageDTO);
        PageResult<Course> pageResult = new PageResult<>();
        PageInfo<Course> coursePageInfo = new PageInfo<>(courses);
        pageResult.setTotal(coursePageInfo.getTotal());
        pageResult.setRows(coursePageInfo.getList());
        return pageResult;
    }

    @Override
    public void addCourse(Course course) {
        //参数校验
        if (Objects.isNull( course)){
            throw new RuntimeException("参数不能为空");
        }
        course.setUpdateTime(LocalDateTime.now());
        course.setCreateTime(LocalDateTime.now());
        courseMapper.addCourse(course);
    }

    @Override
    public void deleteCourseById(Integer id) {
        //参数校验
        if (id == null || id < 1) {
            throw new RuntimeException("参数错误");
        }
        courseMapper.deleteCourseById(id);
    }

    @Override
    public Course selectCourseById(Integer id) {
        //参数校验
        if (id == null || id < 1) {
            throw new RuntimeException("参数错误");
        }
        return courseMapper.selectCourseById(id);
    }

    @Override
    public void updateCourse(Course course) {
        //参数校验
        if (Objects.isNull(course)){
            throw new RuntimeException("参数不能为空");
        }
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.updateCourse(course);

    }
}
